package spring.cloud.tasks.api.database.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import spring.cloud.tasks.api.database.entity.NameSpaceZooKeeperClusterMappingDto;
import spring.cloud.tasks.api.database.repository.NameSpaceZooKeeperClusterMappingDtoRepository;
import spring.cloud.tasks.api.database.service.NameSpaceZooKeeperClusterMappingDtoService;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


@Service
public class NameSpaceZooKeeperClusterMappingDtoServiceImpl implements NameSpaceZooKeeperClusterMappingDtoService {

    @Autowired
    private NameSpaceZooKeeperClusterMappingDtoRepository spaceZooKeeperClusterMappingDtoRepository;


    @Override
    @Transactional

    public Integer insert(String zooKeeperClusterKey, String nameSpace) {
        NameSpaceZooKeeperClusterMappingDto dto = spaceZooKeeperClusterMappingDtoRepository.selectByNameSpaceKey(nameSpace);
        boolean insert;
        if (dto == null) {
            dto = new NameSpaceZooKeeperClusterMappingDto();
            insert = true;
        } else {
            insert = false;
        }
        Date now = new Date();
        dto.setCreatedBy("createdBy");
        dto.setCreateDateTime(now);
        dto.setLastUpdatedBy("createdBy");
        dto.setLastUpdateDateTime(now);
        //
        dto.setNameSpaceKey(nameSpace);

        dto.setZooKeeperClusterKey(zooKeeperClusterKey);
        dto.setIsEffective(0);
        if (insert) {
            return spaceZooKeeperClusterMappingDtoRepository.insert(dto);
        } else {
            return spaceZooKeeperClusterMappingDtoRepository.updateByNameSpaceKey(dto);
        }
    }


    @Transactional
    @Override
    public Integer update(String zooKeeperClusterKey, String nameSpace) {
        NameSpaceZooKeeperClusterMappingDto dto = spaceZooKeeperClusterMappingDtoRepository.selectByNameSpaceKey(nameSpace);

        if (zooKeeperClusterKey != null) {
            dto.setZooKeeperClusterKey(zooKeeperClusterKey);
        }

        return spaceZooKeeperClusterMappingDtoRepository.updateByNameSpaceKey(dto);
    }

    @Transactional
    @Override
    public Integer remove(String nameSpace, String lastUpdatedBy) {
        NameSpaceZooKeeperClusterMappingDto dto = spaceZooKeeperClusterMappingDtoRepository.selectByNameSpaceKey(nameSpace);
        dto.setIsEffective(1);
        dto.setLastUpdatedBy(lastUpdatedBy);
        return spaceZooKeeperClusterMappingDtoRepository.updateByNameSpaceKey(dto);
    }


    @Transactional(readOnly = true)
    @Override
    public List<NameSpaceZooKeeperClusterMappingDto> getEffectiveNameSpaceZooKeeperClusterMappingDtoList() {
        return getEffectiveNameSpaceZooKeeperClusterMappingDtoList(spaceZooKeeperClusterMappingDtoRepository.selectAll());
    }

    @Transactional(readOnly = true)
    @Override
    public List<NameSpaceZooKeeperClusterMappingDto> getEffectiveNameSpaceZooKeeperClusterMappingDtoList(String zooKeeperClusterKey) {
        return getEffectiveNameSpaceZooKeeperClusterMappingDtoList(spaceZooKeeperClusterMappingDtoRepository.selectByZooKeeperClusterKey(zooKeeperClusterKey));
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getNameSpaceList(String zooKeeperClusterKey) {
        List<String> result = new ArrayList<String>();
        List<NameSpaceZooKeeperClusterMappingDto> dtoList = getEffectiveNameSpaceZooKeeperClusterMappingDtoList(spaceZooKeeperClusterMappingDtoRepository.selectByZooKeeperClusterKey(zooKeeperClusterKey));
        for (NameSpaceZooKeeperClusterMappingDto dto : dtoList) {
            result.add(dto.getNameSpaceKey());
        }
        return result;
    }

    private List<NameSpaceZooKeeperClusterMappingDto> getEffectiveNameSpaceZooKeeperClusterMappingDtoList(List<NameSpaceZooKeeperClusterMappingDto> dtoList) {
        List<NameSpaceZooKeeperClusterMappingDto> list = new ArrayList<>();
        if (dtoList != null) {
            Iterator<NameSpaceZooKeeperClusterMappingDto> iterator = dtoList.iterator();
            while (iterator.hasNext()) {
                NameSpaceZooKeeperClusterMappingDto next = iterator.next();
                if (next.getIsEffective() == 0) {
                    list.add(next);
                }
            }
        }
        return list;
    }

    @Transactional(readOnly = true)
    @Override
    public String getZooKeeperClusterKey(String nameSpace) {
        NameSpaceZooKeeperClusterMappingDto dto = spaceZooKeeperClusterMappingDtoRepository.selectByNameSpaceKey(nameSpace);
        if (dto == null) {
            return null;
        }
        if (dto.getIsEffective() == 0) {
            return null;
        }
        return dto.getZooKeeperClusterKey();
    }

}
